package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.redis.service.ITravelStratisVoRedisService;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private ITravelCommentService travelCommentService;

    @Autowired
    private ITravelStratisVoRedisService travelStratisVoRedisService;

    @GetMapping("/query")
    public Object query(TravelQuery qo){
        // 公开且审核
        qo.setStateReleaseAndPublic("YES");
        // page
        Page<Travel> query = travelService.query(qo);
        return JsonResult.success(query);
    }

    @GetMapping("/detail")
    public Object detail(String id){
        if (StringUtils.hasLength(id)) {
            Travel travel = travelService.get(id);
            // detail.author.headImgUrl
            UserInfo userInfo = userInfoService.get(travel.getUserId());
            travel.setAuthor(userInfo);
            // dest.id
            Destination destination = destinationService.get(travel.getDestId());
            travel.setDest(destination);
            return JsonResult.success(travel);
        }else {
            return JsonResult.success();
        }
    }

    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel, @UserParam UserInfo userInfo){
        // 作者id  使用参数解析器(在controller中获取并设置进去)
        travel.setUserId(userInfo.getId());

        travelService.saveOrUpdate(travel);
        return JsonResult.success(travel.getId());
    }

    // 百度UEditor 上传图片 upfile
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }

    @RequireLogin
    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment travelComment, @UserParam UserInfo userInfo){
        BeanUtils.copyProperties(userInfo,travelComment);
        travelComment.setUserId(userInfo.getId());
        travelCommentService.addComment(travelComment);
        return JsonResult.success();
    }

    @GetMapping("/comments")
    public Object comments(String travelId){
        return JsonResult.success(travelCommentService.query(travelId));
    }

    @GetMapping("/commentsNew10")
    public Object commentsNew10(){
        return JsonResult.success(travelCommentService.getCommentsNew10());
    }

    @GetMapping("/statisVo")
    public Object getStatisVo(String tid){
        return JsonResult.success(travelService.getStatisVo(tid));
    }

    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String tid, @UserParam UserInfo userInfo){
        boolean ret = travelStratisVoRedisService.favornumIncrease(tid,userInfo.getId());
        return JsonResult.success(ret);
    }

    @RequireLogin
    @PostMapping("/travelThumbup")
    public Object travelThumbup(String tid, @UserParam UserInfo userInfo){
        boolean ret = travelStratisVoRedisService.thumbupnumIncrease(tid,userInfo.getId());
        return JsonResult.success(ret);
    }

}
